<template>
  <div class="home_container">
    <div class="home_container_one">
      <div class="home-sort">
        <div>Sort by:</div>
        <div :class="selectindex == '1' ? 'active1' : 'back1' " @click="sortClick('1')">Default</div>
        <div :class="selectindex == '2' ? 'active1' : 'back1' " @click="sortClick('2')">
          Parice
          <span v-show="show">↑</span>
          <span v-show="!show">↓</span>
        </div>
        <div @click="toCart">
          <van-icon name="shopping-cart-o" size="25" :badge="this.$store.state.cartItem.length" />
        </div>
      </div>
    </div>
    <div class="home_container_two">
      <div class="home_container_two_left">
        <p>PRICE:</p>
        <div
          v-for="(item,index) in oneNav"
          :key="index"
          :class="slectIndex == index ? 'active' : 'back' "
          @click="selectIndex(index,item)"
        >{{item.title}}</div>
      </div>
      <div class="home_container_two_right">
        <el-row :gutter="10">
          <el-col
            :xs="24"
            :sm="12"
            :md="8"
            :lg="6"
            :xl="4"
            v-for="(item,index) in showList"
            :key="index"
          >
            <div class="grid-content">
              <div class="grid-img">
                <img :src="item.productImage" alt />
              </div>
              <div class="grid-name">{{item.productName}}</div>
              <div class="grid-price">{{item.salePrice | addCurrencyTag}}</div>
              <div class="grid-addcart" @click="addCart(item)">加入购物车</div>
            </div>
          </el-col>
        </el-row>
        <div class="homr-rught-img" v-photoShow="isLoading">
          <img src="http://localhost:8080/素材/img/timg.gif" alt />
        </div>
      </div>
    </div>
    <!-- 模态框 -->
    <van-overlay :show="isshow" @click="isshow = false">
      <div class="wrapper" @click.stop>
        <div
          style="width:400px;height:200px;background-color:white;margin: 20% auto;overflow: hidden;"
        >
          <div
            style="width:100%;height:30px;line-height:30px;color:#333333;text-align:center;margin-top:20px;"
          >加入购物车成功！！！</div>
          <div
            style="width:100%;height:40px;display:flex;justify-content: space-around;margin-top:60px;"
          >
            <div
              style="margin-right:10px;width:180px;height:40px;border:1px solid red;background-color:#FFE8DF;color:red;text-align:center;line-height:40px;"
              @click="isshow = false"
            >继续购物</div>
            <div
              style="width:180px;height:40px;border:1px solid red;background-color:#DE5149;color:white;text-align:center;line-height:40px;"
              @click="toCart"
            >查看购物车</div>
          </div>
        </div>
      </div>
    </van-overlay>
  </div>
</template>

<script>
export default {
  data() {
    return {
      slectIndex: 0, //左侧的点击效果
      isLoading: false, //图片的出来
      page: 1,
      selectindex: 1,
      show: false,
      isshow: false,
    };
  },
  //  过滤器，用于价钱的筛选
  filters: {
    addCurrencyTag(value) {
      var tempPrice = parseFloat(value).toFixed(2);
      return `¥${tempPrice}`;
    }
  },
  methods: {
    //   下标切换
    selectIndex(index, item) {
      this.slectIndex = index;
      this.$store.dispatch("SELECTINDEX", item);
    },
    // 排序
    sortClick(index) {
      this.selectindex = index;
      if (index == "1") {
        this.$store.dispatch("GOODS_DATA");
      }
      if (index == "2") {
        this.sort = !this.sort;
        if (this.sort) {
          this.show = !this.show;
          this.$store.state.showList.sort((a, b) => {
            return a.salePrice - b.salePrice;
          });
        } else {
          this.show = !this.show;
          this.$store.state.showList.sort((a, b) => {
            return b.salePrice - a.salePrice;
          });
        }
      }
    },
    // 添加购物车
    addCart(item) {
        this.isshow = !this.isshow;
      this.$store.dispatch("ADDCART_DATA", item);
    },
    //去到购物车页面
    toCart() {
      this.$router.push({
        path: "/cart"
      });
    }
  },
  mounted() {
    // 所有的数据
    this.$store.dispatch("GOODS_DATA");
    // 刷新拿数据
    window.addEventListener("scroll", () => {
      // 获取文档的高度
      var docHeight = document.documentElement.scrollHeight;
      // 获取当前窗口的高度(可视区域)
      var client = document.documentElement.clientHeight;
      // 获取滚动的距离
      var scrollOffset = document.documentElement.scrollTop;
      // 拿到差值
      var zhi = docHeight - client - scrollOffset;
      if (zhi == 0) {
        window.console.log("已经达到文档的底部");
        this.isLoading = true;
        // 执行异步请求
        window.setTimeout(() => {
          this.$store.dispatch("GET_XIN_DATA", ++this.page);
          this.isLoading = false;
        }, 3000);
      }
    });
  },
  computed: {
    oneNav() {
      return this.$store.state.oneNav;
    },
    showList() {
      return this.$store.state.showList;
    }
  },
  //   自定义指令
  directives: {
    photoShow: {
      bind() {},
      inserted: function(el, binding) {
        //el:指令绑定的元素
        //binding.name指令名	binding.value绑定值	bingding.expression绑定至(字符串形式)
        if (binding.value) {
          el.style.visibility = "visible";
        } else {
          el.style.visibility = "hidden";
        }
      },
      update: function(el, binding) {
        //el:指令绑定的元素
        //binding.name指令名	binding.value绑定值	bingding.expression绑定至(字符串形式)
        if (binding.value) {
          el.style.visibility = "visible";
        } else {
          el.style.visibility = "hidden";
        }
      }
    }
  }
};
</script>

<style scoped>
.home_container {
  width: 100%;
  height: 100%;
}
.home_container_one {
  width: 100%;
  height: 80px;
  background-color: #f0f0f0;
  position: fixed;
  top: 0;
  left: 0;
  line-height: 80px;
}
.home-sort {
  width: 18%;
  height: 100%;
  float: right;
  display: flex;
}
.home-sort div {
  margin-left: 6px;
}
.active1 {
  color: red;
}
.back1 {
  color: gray;
}
.home_container_two {
  width: 100%;
  height: 900px;
  display: flex;
  margin-top: 80px;
}
.home_container_two_left {
  width: 300px;
  height: 100%;
}
.home_container_two_left p {
  width: 100%;
  height: 50px;
  margin-left: 20px;
  line-height: 50px;
  color: gray;
}
.home_container_two_left div {
  width: 100%;
  height: 45px;
  line-height: 45px;
  margin-left: 20px;
  font-size: 20px;
}
.back {
  color: gray;
  border-left: 3px solid white;
}
.active {
  border-left: 3px solid orange;
  text-indent: 20px;
  color: orange;
}
.home_container_two_right {
  width: 80%;
  height: 100%;
  overflow: hidden;
  overflow-x: hidden;
  overflow-y: scroll;
}
.grid-content {
  height: 400px;
  background-color: white;
  border: 1px solid orange;
  margin: 10px 0;
}
.grid-content:hover {
  box-shadow: 0px 2px 10px orange;
  transition: all 0.2s ease-in-out;
  transform: translateY(-5px);
}
@media screen and (max-width: 768px) {
  .grid-content {
    width: 100%;
    height: 100px;
    background-color: white;
    margin: 10px 0;
    display: flex;
  }
  .grid-content .grid-img {
    width: 20% !important;
    height: 100px !important;
  }
  .grid-content .grid-img img {
    width: 100% !important;
    height: 100% !important;
  }
  .grid-content > .grid-name {
    font-size: 18px !important;
    color: #333 !important;
    /* text-indent: 10px !important; */
    line-height: 100px;
  }
  .grid-content > .grid-price {
    font-size: 22px !important;
    color: red !important;
    font-weight: 500 !important;
    text-indent: 10px !important;
    line-height: 70px !important;
    margin-left: 20px;
  }
  .grid-content > .grid-addcart {
    width: 20% !important;
    height: 30px !important;
    border: 2px solid orange !important;
    line-height: 30px !important;
    text-align: center !important;
    margin: 0 auto !important;
    color: red !important;
    font-size: 18px !important;
    margin-top: 30px !important;
  }
  .home_container_two_left {
    display: none;
  }
  .home_container_two_right {
    width: 100%;
    overflow: hidden;
    overflow-x: hidden;
    overflow-y: scroll;
  }
}
.grid-content .grid-img {
  width: 100%;
  height: 280px;
}
.grid-content .grid-img img {
  width: 100%;
  height: 100%;
}
.grid-content > .grid-name {
  font-size: 18px;
  color: #333;
  text-indent: 10px;
}
.grid-content > .grid-price {
  font-size: 22px;
  color: red;
  font-weight: 500;
  text-indent: 10px;
  margin-top: 20px;
}
.grid-content > .grid-addcart {
  width: 95%;
  height: 30px;
  border: 2px solid orange;
  line-height: 30px;
  text-align: center;
  margin: 0 auto;
  color: red;
  font-size: 18px;
  margin-top: 5px;
}
.homr-rught-img {
  margin: 0 auto;
}
</style>